//
//  MainViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/6.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "MainViewController.h"

#import "MusicViewController.h"
#import "SeachviewController.h"
#import "RecommedViewController.h"
#import "SingleViewController.h"
#import "AudioPlayerView.h"

#import "RadioViewController.h"

@interface MainViewController ()
{
    STKAudioPlayer* audioPlayer;
    // 四大视图
    SeachviewController *_searchVc;
    RecommedViewController *_recommedVc;
    MusicViewController *_musicVc;
    RadioViewController *_radioVc;

    
    UIScrollView *_scrollView;
    TitleView *_titleView;
    NSInteger count;
    AudioPlayerView *MyAudioPlayer;
    DanLiPlayer *BarPlayer;
    MBProgressHUD *HUD;
    
}
@end

@implementation MainViewController

- (void)dealloc
{
    [super dealloc];
}



- (void)viewWillAppear:(BOOL)animated
{
    // 单例播放器
    audioPlayer = [SinglePlayer shareHandleSinglePlayer];

    // 播放Bar
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    BarPlayer.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50);
    BarPlayer.delegate = self;
    [self.view addSubview:BarPlayer];    
    
    // 判断 我的播放按钮 状态
    if (audioPlayer.state == STKAudioPlayerStatePlaying) {
        // 暂停
        [BarPlayer.Play setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:0];
        
    } else {
        // 播放
        [BarPlayer.Play setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:0];
    
    }
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self addChildMyViewControllers];
    
}

- (void)addChildMyViewControllers
{

    self.view.backgroundColor = [UIColor whiteColor];
    // 取消系统的scrollview 自适应
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    /**
     *  下标的初始化
     */

    count = 0;
    
    // 四个主页面
    _searchVc = [[SeachviewController alloc] init];
    _recommedVc = [[RecommedViewController alloc] init];
    _radioVc = [[RadioViewController alloc] init];
    _musicVc = [[MusicViewController alloc] init];
    
    [self addChildViewController:_searchVc];
    [self addChildViewController:_radioVc];
    [self addChildViewController:_recommedVc];
    [self addChildViewController:_musicVc];


    [_searchVc release];
    [_recommedVc release];
    [_radioVc release];
    [_musicVc release];
    
    
    // 添加在scrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:SCREENFRAME];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*4, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setShowsVerticalScrollIndicator:NO];
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
    // 初始化坐标
    _recommedVc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _musicVc.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _searchVc.view.frame = CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _radioVc.view.frame = CGRectMake(SCREEN_WIDTH*3, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [_scrollView addSubview:_searchVc.view];
    [_scrollView addSubview:_radioVc.view];
    [_scrollView addSubview:_recommedVc.view];
    [_scrollView addSubview:_musicVc.view];
    
    // titleView
    _titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    _titleView.myChangeDelegate = self;
    self.navigationItem.titleView = _titleView;
    [_titleView release];
    
    // 抽屉的按钮
    UIBarButtonItem *leftListButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"liebiao_32"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(Drawer)];

    self.navigationItem.leftBarButtonItem = leftListButton;
    
}


// 控制滑动
- (void)changeOffSet:(NSInteger)tag
{
    
    NSInteger tagCount = tag-10001;
    [UIView animateWithDuration:0.3 animations:^{
        
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH*tagCount, 0)];
        
    } completion:^(BOOL finished) {
        
    }];
    
    
}
// 协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = _scrollView.contentOffset.x / SCREEN_WIDTH;    
    for (UIButton * btn in _titleView.buttonArr) {
        if (page == btn.tag-10001) {

            btn.titleLabel.textColor = COLOR(181, 50, 124, 1.0);
            
        } else {
            btn.titleLabel.textColor = [UIColor whiteColor];
        }
    }
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
         _titleView.titleView.frame = CGRectMake(SCREEN_WIDTH / 5 * page+10 , 41, SCREEN_WIDTH / 7, 3);
    } completion:^(BOOL finished) {
        
    }];   

}


// 下一曲
- (void)nextMusicButton:(NSMutableArray *)reDetailModelArr index:(NSInteger)index
{
    
    // 数组部位空的时候 获取当前的index
    if ([reDetailModelArr count]!=0) {
        count = index;
        count += 1;
        // 如果 下标到了 最大值 减1 (0 ~ 14)
    if (index == [reDetailModelArr count]-1) {
            count = 0;
        }
        
        // 传值, 数组和model类, 下标
        ReDetailModel *model = [reDetailModelArr objectAtIndex:count];
        BarPlayer.reDetailModel = model;
        BarPlayer.dataArr = reDetailModelArr;
        BarPlayer.index = count;
        
        // 不同的model 显示不同的数据
        if (model.singerName) {
            NSDictionary *dic = [model.auditionList objectAtIndex:1];
            NSString *url = [dic objectForKey:@"url"];
            [audioPlayer play:url];
            
        } else {
            NSDictionary *dic = [model.audition_list objectAtIndex:1];
            NSString *url = [dic objectForKey:@"url"];
            [audioPlayer play:url];
        }
    } else {
       
        // 提示用户
        [self showNoticeforUser];
        
//        NSLog(@"没有歌曲的时候不显示");
        
    }
   
}

#pragma 单例Bar协议方法 显示播放页面
- (void)mainViewAppear:(NSMutableArray *)ModelArr index:(NSInteger)index playWays:(NSInteger)playWays
{
    
    if (ModelArr.count > 0) {
        
        PlayerViewController *playVc = [PlayerViewController shareHandleViewController];
        playVc.dataArr = ModelArr;
        playVc.index = index;
        playVc.playWays = playWays;
        [self presentViewController:playVc animated:YES completion:nil];
    } else {
        
        // 提示用户
        [self showNoticeforUser];
        
    }
    
    
}

// 提示
- (void)showNoticeforUser
{
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.mode = MBProgressHUDModeText;
    [HUD show:YES];
    [HUD hide:YES afterDelay:3];
    [self.view addSubview:HUD];
    HUD.labelText = @"没有任何歌曲,去歌单看看吧~";
}


// 播放列表
- (void)changeView:(ReDetailModel *)reDetailModel dataArr:(NSMutableArray *)dataArr
{
    // 播放列表
    if (dataArr.count > 0) {
        
        SingleViewController *Vc = [[[SingleViewController alloc] init] autorelease];
        Vc.reDetailModel = reDetailModel;
        Vc.dataArr = dataArr;
        [self presentViewController:Vc animated:YES completion:nil];
    } else {
        
        
        [self showNoticeforUser];
        
    }
}

// 播放与暂停
- (void)playButton:(ReDetailModel *)reDetailModel
{
    //  暂停与播放
    if (!audioPlayer){
        return;
    }
    if (audioPlayer.state == STKAudioPlayerStatePaused){        
        
        [audioPlayer resume];
        [BarPlayer.Play setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:0];
        
    }else{
        [audioPlayer pause];
        [BarPlayer.Play setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:0];
    }
    
    
    
}

// 抽屉左键按钮 引入 appdelegate
- (void)Drawer
{
    // 通过app 回到主Window用来显示抽屉的VC
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    // 由关闭状态打开抽屉
    if (tempAppDelegate.leftVc.closed)
    {
        [tempAppDelegate.leftVc openLeftView];
    }
    // 关闭抽屉
    else
    {
        [tempAppDelegate.leftVc closeLeftView];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
